# coding: utf-8

import django_filters
from main.models import JwtLog
from rest_framework import mixins, viewsets
from rest_framework.decorators import action

from . import filters, models, serializers


class OpLogViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):

    queryset = models.OpLog.objects.all()
    serializer_class = serializers.OpLogSerializer
    ordering = ("-id",)
    filter_class = filters.OpLogFilter
    search_fields = ("log", "operator", "log_type")

    def get_serializer_class(self):
        return super(OpLogViewSet, self).get_serializer_class()


class JwtLogFilter(django_filters.rest_framework.FilterSet):
    class Meta:
        model = JwtLog
        fields = {
            "create_time": ["range"],
            "update_time": ["range"],
        }


class JwtLogSerializer(serializers.serializers.ModelSerializer):
    user_name = serializers.serializers.CharField(source='user.username', allow_null=True, read_only=True)

    class Meta:
        model = JwtLog
        fields = "__all__"


class JwtLogViewSet(viewsets.ModelViewSet):
    """
    JwtLog管理
    """

    filter_class = JwtLogFilter
    queryset = JwtLog.objects.filter()
    serializer_class = JwtLogSerializer
    ordering = ("-id",)
